<template>
    <div class="edu-page-warp report-auth">
        <condition-card
            @reset="reset"
            @search="onSubmit"
            label-width="130px"
            label-position="right"
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item label="年级">
                        <GradeSelect v-model="query.grade" :show-all="false"></GradeSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="入学季节">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.enrolSeasonList"
                            :data="options.enrolSeason"
                            multiple
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="培养层次">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.trainingLevelList"
                            :data="options.trainingLevel"
                            multiple
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="培养类别">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.trainingCategoryList"
                            :data="options.trainingCategory"
                            multiple
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="录取类别">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.enrolCategoryList"
                            :data="options.enrolCategory"
                            multiple
                        ></DictSelect>
                    </el-form-item>
                </el-col>
            </el-row>
        </condition-card>
        <el-card shadow="never">
            <div class="edu-page-title">新生报到权限管理</div>
            <el-row type="flex" class="mb-8px">
                <el-col class="text-right">
                    <el-button
                        type="primary"
                        @click="addRegistration"
                        v-if="currentUid !== 'admin1'"
                    >{{ $t('message.add') }}</el-button>
                </el-col>
            </el-row>
            <edu-table :order="false" :total="registrationList.total" :data="registrationList.data">
                <el-table-column label="年级" width="100">
                    <template slot-scope="scope">{{ scope.row.grade }}</template>
                </el-table-column>
                <el-table-column label="入学季节">
                    <template slot-scope="scope">{{ scope.row.enrolSeasonI18n }}</template>
                </el-table-column>
                <el-table-column label="培养层次">
                    <template slot-scope="scope">{{ scope.row.trainingLevelI18n }}</template>
                </el-table-column>
                <el-table-column label="培养类别">
                    <template slot-scope="scope">{{ scope.row.trainingCategoryI18n }}</template>
                </el-table-column>
                <el-table-column label="录取类别">
                    <template slot-scope="scope">{{ scope.row.enrolCategoryI18n }}</template>
                </el-table-column>
                <el-table-column label="开始时间">
                    <template
                        slot-scope="scope"
                    >{{ scope.row.startTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template>
                </el-table-column>
                <el-table-column label="结束时间">
                    <template
                        slot-scope="scope"
                    >{{ scope.row.endTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template>
                </el-table-column>
                <el-table-column label="操作" width="90" v-if="currentUid !== 'admin1'">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="editRegistration(scope.row)">编辑</el-button>
                        <el-button
                            type="text"
                            size="mini"
                            @click="deleteRegistration(scope.row)"
                        >{{ $t('message.delete') }}</el-button>
                    </template>
                </el-table-column>
            </edu-table>
        </el-card>
        <!-- 新生报到权限维护（新增、编辑） -->
        <edu-dialog
            ref="registrationDialog"
            title="新生报到权限维护"
            size="medium"
            @close="closeRegistrationDialog"
        >
            <edu-dialog ref="innerDialog" title="提示" class="tip-item" append-to-body>
                返回将不会保存当前信息激活设置，是否确定？
                <span slot="buttons">
                    <el-button @click="cancleInner">取消</el-button>
                    <el-button @click="confirmInner">确定</el-button>
                    <el-button type="primary" @click="saveAndClose">保存并关闭</el-button>
                </span>
            </edu-dialog>
            <el-form
                ref="registrationForm"
                :model="registrationForm"
                label-width="100px"
                label-position="right"
                :rules="rulesForm"
            >
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="年级：" prop="grade">
                            <GradeSelect v-model="registrationForm.grade" :show-all="false"></GradeSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="入学季节：" prop="enrolSeasonList">
                            <DictSelect
                                labelAll="请选择"
                                v-model="registrationForm.enrolSeason"
                                :data="options.enrolSeason"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="培养层次：" prop="trainingLevelList">
                            <DictSelect
                                labelAll="请选择"
                                v-model="registrationForm.trainingLevel"
                                :data="options.trainingLevel"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="培养类别：" prop="trainingCategoryList">
                            <DictSelect
                                labelAll="请选择"
                                v-model="registrationForm.trainingCategory"
                                :data="options.trainingCategory"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="录取类别：" prop="enrolCategory">
                            <DictSelect
                                labelAll="请选择"
                                v-model="registrationForm.enrolCategory"
                                :data="options.enrolCategory"
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="开始时间：" prop="startTime">
                            <el-date-picker
                                style="width: 100%;"
                                v-model="registrationForm.startTime"
                                type="datetime"
                                placeholder="选择日期时间"
                                value-format="timestamp"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="结束时间：" prop="endTime">
                            <el-date-picker
                                style="width: 100%;"
                                v-model="registrationForm.endTime"
                                :picker-options="registrationForm.startTime ? pickerEndTime : ''"
                                type="datetime"
                                placeholder="选择日期时间"
                                value-format="timestamp"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="buttons">
                <el-button @click="cancleRegistrationDialog">返回</el-button>
                <el-button type="primary" @click="addAndEditRegistrater">保存</el-button>
            </span>
        </edu-dialog>
        <!-- 删除弹窗 -->
        <edu-dialog ref="deleteDialog" title="提示" class="tip-item" @close="closeDelete">
            是否确认删除?
            <span slot="buttons">
                <el-button @click="cancleDelete">否</el-button>
                <el-button type="primary" @click="confirmDelete">是</el-button>
            </span>
        </edu-dialog>
    </div>
</template>
<script>
import * as API from "common/src/api/reportAuthMange";
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import deleteDialog from "common/src/views/administrator/studentStatusManage/DeleteItems";
export default {
    components: { deleteDialog },
    data() {
        return {
            query: {
                grade: "",
                enrolSeasonList: [],
                trainingLevelList: [],
                trainingCategoryList: [],
                enrolCategoryList: [],
                pageNum_: 1,
                pageSize_: 20
            },
            options: {
                enrolSeason: [],
                trainingLevel: [],
                trainingCategory: [],
                enrolCategory: []
            },
            registrationForm: {
                grade: "",
                enrolSeason: "",
                trainingLevel: "",
                trainingCategory: "",
                enrolCategory: "",
                startTime: null,
                endTime: null
            },
            // 操作类型 新增或编辑
            operateType: null,
            rulesForm: {
                grade: [
                    { required: true, message: "请选择年级", trigger: "change" }
                ],
                startTime: [
                    {
                        required: true,
                        message: "请选择开始时间",
                        trigger: "change"
                    }
                ]
            },
            pickerEndTime: this.getEndTime(),
            deleteDialog: {
                deleteData: null
            },
            registrationList: {
                data: [],
                total: 0
            },
            currentUid: "" // 当前uid
        };
    },
    created() {
        // 获取当前部门和当前uid
        if (this.mockUser) {
            this.currentUid = this.mockUser.uid;
        } else {
            this.currentUid = this.currentUser.uid;
        }
    },
    mounted() {
        this.initOptions();
        this.onSubmit();
    },
    methods: {
        initOptions() {
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_RXJJ", "X_PYCC", "X_PYLB", "X_LQLB"]
            };
            queryDic(data).then(res => {
                if (res.code === 200 && res.data) {
                    UTILS.fillSelect(
                        {
                            enrolSeason: "X_RXJJ",
                            trainingLevel: "X_PYCC",
                            trainingCategory: "X_PYLB",
                            enrolCategory: "X_LQLB"
                        },
                        res.data,
                        this.options
                    );
                }
            });
        },
        onSubmit() {
            API.getRegistrationList(this.query).then(res => {
                let { code, data } = res;
                if (code === 200 && data) {
                    this.registrationList = {
                        data: data.list,
                        total: data.total_
                    };
                    // this.reportAuths = data;
                }
            });
        },
        reset() {
            this.initQuery();
            this.onSubmit();
        },
        // 初始化query对象
        initQuery() {
            let query = this.query;
            for (var key in query) {
                if (key === "pageNum_") {
                    query[key] = 1;
                } else if (key === "pageSize_") {
                    query[key] = 20;
                } else {
                    if (query[key] instanceof Array) {
                        query[key] = [];
                    } else {
                        query[key] = "";
                    }
                }
            }
        },
        // 新增
        addRegistration() {
            this.operateType = "add";
            this.openRegistrationDialog();
        },
        // 打开registrationDialog
        openRegistrationDialog() {
            this.$refs.registrationDialog.open();
        },
        // 取消registrationDialog
        cancleRegistrationDialog() {
            let formData = this.registrationForm;
            let keys = Object.keys(formData).filter(item => {
                return formData[item];
            });
            let length = keys.length;
            if (length) {
                this.$refs.innerDialog.open();
            } else {
                this.$refs.registrationDialog.close();
            }
        },
        // 关闭registrationDialog时触发
        closeRegistrationDialog() {
            // 初始化registrationForm
            this.registrationForm = {
                grade: "",
                enrolSeason: "",
                trainingLevel: "",
                trainingCategory: "",
                enrolCategory: "",
                startTime: null,
                endTime: null
            };
            this.$refs.registrationForm.resetFields();
        },
        // 关闭并保存（innerDialog和registrationDialog）
        saveAndClose() {
            this.addAndEditRegistrater();
        },
        // 取消 innerDialog
        cancleInner() {
            this.$refs.innerDialog.close();
        },
        // 确定 innerDialog
        confirmInner() {
            this.cancleInner();
            this.$refs.registrationDialog.close();
        },
        // 新增、编辑权限维护
        addAndEditRegistrater() {
            this.$refs.registrationForm.validate(valid => {
                if (valid) {
                    if (
                        (this.registrationForm.endTime &&
                            this.registrationForm.endTime >
                                this.registrationForm.startTime) ||
                        !this.registrationForm.endTime
                    ) {
                        if (this.operateType && this.operateType === "add") {
                            API.addReportConfig(this.registrationForm).then(
                                res => {
                                    if (res.code === 200) {
                                        this.$message({
                                            type: "success",
                                            message: res.msg || "新增权限成功"
                                        });
                                        this.$refs.registrationDialog.close();
                                        this.onSubmit();
                                    }
                                }
                            );
                        } else if (
                            this.operateType &&
                            this.operateType === "edit"
                        ) {
                            API.updateReportConfig(this.registrationForm).then(
                                res => {
                                    if (res.code === 200) {
                                        this.$message({
                                            type: "success",
                                            message: res.msg || "编辑权限成功"
                                        });
                                        this.confirmInner();
                                        this.onSubmit();
                                    }
                                }
                            );
                        }
                    } else {
                        this.$message({
                            type: "warning",
                            message: "结束时间必须大于开始时间"
                        });
                    }
                } else {
                    this.cancleInner();
                }
            });
        },
        // 删除按钮
        deleteRegistration(row) {
            this.$refs.deleteDialog.open();
            this.deleteDialog.deleteData = row;
        },
        // 关闭deleteDialog时触发方法
        closeDelete() {
            this.deleteDialog.deleteData = null;
        },
        // 取消删除
        cancleDelete() {
            this.$refs.deleteDialog.close();
        },
        // 确定删除
        confirmDelete() {
            let deleteData = this.deleteDialog.deleteData;
            if (deleteData && deleteData.id)
                API.deleteReportConfig([deleteData.id]).then(res => {
                    if (res.code === 200) {
                        this.$message({
                            type: "success",
                            message: res.msg || "删除成功"
                        });
                        this.cancleDelete();
                        this.onSubmit();
                    }
                });
        },
        // 编辑
        editRegistration(row) {
            this.operateType = "edit";
            this.openRegistrationDialog();
            this.registrationForm = {
                grade: row.grade,
                enrolSeason: row.enrolSeason,
                trainingLevel: row.trainingLevel,
                trainingCategory: row.trainingCategory,
                enrolCategory: row.enrolCategory,
                startTime: row.startTime ? row.startTime : null,
                endTime: row.endTime ? row.endTime : null
            };
            this.registrationForm.id = row.id;
        },
        // 结束时间（禁用项）
        getEndTime() {
            let _this = this;
            return {
                disabledDate(time) {
                    if (_this.registrationForm.startTime)
                        return (
                            time.getTime() < _this.registrationForm.startTime
                        );
                }
            };
        }
    },
    computed: {
        currentUser() {
            if (this.$store.state.session.user) {
                return this.$store.state.session.user;
            }
            return JSON.parse(localStorage.getItem("user"));
        },
        mockUser() {
            if (this.$store.state.session.mockUser) {
                return this.$store.state.session.mockUser;
            }
            return JSON.parse(localStorage.getItem("sessiondata")).mockUser;
        }
    }
};
</script>
<style lang="scss">
</style>

